package com.yksj.healthtalk.adapter;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.AppData;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.PersonInfoUtil;

public class DoctorServiceDataListAdapter extends BaseAdapter {
	public JSONArray mArray = new JSONArray();
	private ImageLoader mImageLoader;
	private LayoutInflater mInflater;
	private Context context;
	private Drawable attenDrawable, notAttenDrawable;

	onClickFriendAttentionListener attentionListener;
	private AppData appData;

	public interface onClickFriendAttentionListener {
		void onClickFriendAttention(int attentioned, CustomerInfoEntity entity);
	}

	public void setonClickFriendAttentionListener(
			onClickFriendAttentionListener attentionListener) {
		this.attentionListener = attentionListener;
	};

	public DoctorServiceDataListAdapter(Context context) {
		this.context = context;
		mImageLoader = ImageLoader.getInstance();
		attenDrawable = context.getResources()
				.getDrawable(R.drawable.attention);
		notAttenDrawable = context.getResources().getDrawable(
				R.drawable.not_attention);
		mInflater = (LayoutInflater) context
				.getSystemService(context.LAYOUT_INFLATER_SERVICE);
		appData= HTalkApplication.getAppData();
	}

	public void onDataChange(JSONArray array) {
		this.mArray = array;
		notifyDataSetChanged();
	}

	public void onAddEndList(JSONArray array) {
		mArray.addAll(array);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mArray.size();
	}

	@Override
	public JSONObject getItem(int position) {
		return mArray.getJSONObject(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = onCeateView();
		}
		final ViewHolder holder = (ViewHolder) convertView.getTag();
		onBindView(holder, getItem(position));
		return convertView;
	}

	private View onCeateView() {
		ViewHolder mHolder = new ViewHolder();
		View convertView = mInflater.inflate(R.layout.friendlist_item1, null);
		mHolder.headerImageView = (ImageView) convertView
				.findViewById(R.id.head_image);
		mHolder.nameTextView = (TextView) convertView.findViewById(R.id.name);
		mHolder.sexImageView = (ImageView) convertView
				.findViewById(R.id.head_sex);
		mHolder.levImageView = (ImageView) convertView.findViewById(R.id.levl);
		mHolder.attention = (ImageView) convertView
				.findViewById(R.id.attention);
		mHolder.choose = (CheckBox) convertView.findViewById(R.id.choose);
		mHolder.noteTextView = (TextView) convertView.findViewById(R.id.note);
		convertView.setTag(mHolder);
		mHolder.itemDele = (TextView) convertView.findViewById(R.id.item_dele);
		return convertView;
	}

	/**
	 * 绑定数据
	 * 
	 * @param holder
	 * @param type
	 * @param jsonObject
	 * 
	 */
	private void onBindView(ViewHolder holder, final JSONObject jsonObject) {
		String nickName = jsonObject.getString("customerNickname");
		String personalNarrate = jsonObject.getString("personalNarrate");//显示个性签名
		if (nickName != null && nickName.length() != 0) {
			holder.nameTextView.setText(nickName + "("
					+ jsonObject.getString("customerAccounts") + ")");
		} else {
			holder.nameTextView.setText(jsonObject
					.getString("customerAccounts"));
		}
		if (!"".equals(jsonObject.getString("remarksName"))) {
			holder.nameTextView.setText(jsonObject.getString("remarksName"));
		}
		if(!"".equals(personalNarrate)&& personalNarrate.length()>0 ){
			holder.noteTextView.setText(personalNarrate);
		}
		mImageLoader.displayImage(jsonObject.getString("customerSex"),
				jsonObject.getString("clientIconBackground"),
				holder.headerImageView);

		LevelListDrawable levelListDrawable = (LevelListDrawable) holder.sexImageView
				.getDrawable();
		levelListDrawable.setLevel(jsonObject.getIntValue("customerSex"));
		holder.headerImageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				PersonInfoUtil.choiceActivity(
						jsonObject.getString("customerId"), context,
						jsonObject.getString("roleId"));
			}
		});
		final CustomerInfoEntity entity = new CustomerInfoEntity();
		entity.setId(jsonObject.getString("customerId"));
		if ("".equals(jsonObject.getString("remarksName"))) {
			entity.setName(jsonObject.getString("customerNickname"));
		} else {
			entity.setName(jsonObject.getString("remarksName"));
		}// relationType
		entity.setIsAttentionFriend(jsonObject.getIntValue("relationType"));
		holder.choose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				FriendHttpUtil.requestAttentionTofriends(context, null, entity);
			}
		});

		holder.attention.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (attentionListener != null) {
					attentionListener.onClickFriendAttention(
							entity.getIsAttentionFriend(), entity);
				}
			}
		});
		if (entity.getIsAttentionFriend() != 0
				&& entity.getIsAttentionFriend() != 2) {
			holder.attention.setImageDrawable(notAttenDrawable);
		} else {
			holder.attention.setImageDrawable(attenDrawable);
		}
		//未读消息
		int size = appData.getNoReadMesgSize( jsonObject.getString("customerId"));
		if (size != 0) {
			holder.itemDele.setVisibility(View.VISIBLE);
			String readCount=size >100 ? "99+":String.valueOf(size);
			holder.itemDele.setText(readCount);
		} else {
			holder.itemDele.setVisibility(View.GONE);
		}
	}

	static class ViewHolder {
		ImageView attention;
		ImageView headerImageView;
		TextView nameTextView;
		ImageView sexImageView;
		ImageView levImageView;
		TextView noteTextView;
		TextView departmentsTextView;
		TextView specialTextView;
		TextView hospitalTextView;
		ImageView doctorVImageView;
		ImageView moneyImageView;
		CheckBox choose;// 是否关注
		TextView msgTextView;
		TextView itemDele;
	}
}